package net.xdclass.controller;

import net.xdclass.controller.req.AccountLoginReq;
import net.xdclass.controller.req.AccountRegisterReq;
import net.xdclass.dto.AccountDTO;
import net.xdclass.interceptior.LoginInterceptor;
import net.xdclass.service.AccountService;
import net.xdclass.utils.JWTUtil;
import net.xdclass.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/account/v1")
public class AccountController {

    @Autowired
    private AccountService accountService;


    /**
     * 注册接口
     */
    @RequestMapping("/register")
    public Object register(@RequestBody AccountRegisterReq req){
        accountService.register(req);
        return JsonData.buildSuccess();
    }

    /**
     * 头像上传接口
     */
    @PostMapping("upload_avatar")
    public Object uploadAvatar(@RequestParam("file") MultipartFile file){

        String url = accountService.updateAvatar(file);

       return JsonData.buildSuccess(url);
    }

    /**
     * 登录模块
     * @param req
     * @return
     */
    @PostMapping("login")
    public JsonData login(@RequestBody AccountLoginReq req){
        AccountDTO accountDTO = accountService.login(req);

        //生成token
        String token = JWTUtil.geneJsonWebTokne(accountDTO);
        return JsonData.buildSuccess(token);
    }

    /**
     * 获取用户详情接口
     */
    @GetMapping("detail")
    public JsonData detail(){
        AccountDTO accountDTO = accountService.queryDetail(LoginInterceptor.threadLocal.get().getId());
        return JsonData.buildSuccess(accountDTO);
    }
}



